<?php

namespace App\Models;

use App\Util\Url;
use Illuminate\Database\Eloquent\Model;

class TaskHall extends Model
{
    protected $table = "c_task_hall";


    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 成长值信息
     */
    public static function growthValueInformation($filter)
    {
        $work = DoctorWorks::where(["id" => $filter['work_id']])->first();
        $bronze = config("omnipay.growsNum.bronze");
        $silver = config("omnipay.growsNum.silver");
        $gold = config("omnipay.growsNum.gold");
        $lit['exist'] = $work->growth_value;
        if ($work->growth_value >= $bronze && $work->growth_value < $silver) { //银牌
            $lit['name'] = "银牌工作室";
            $lit['remain'] = $silver - $work->growth_value;
            $lit['total'] = $silver;
            if ($work->medal_type != 1) {
                DoctorWorks::where(["id" => $filter['work_id']])->update(['medal_type' => 1]);
            }
        } elseif ($work->growth_value >= $gold) { //金牌
            $lit['name'] = "金牌工作室";
            $lit['remain'] = 0;
            $lit['total'] = $gold;
            if ($work->medal_type != 2) {
                DoctorWorks::where(["id" => $filter['work_id']])->update(['medal_type' => 2]);
            }
        } else { //铜牌
            $lit['name'] = "铜牌工作室";
            $lit['remain'] = $bronze - $work->growth_value;
            $lit['total'] = $bronze;
            if ($work->medal_type != 0) {
                DoctorWorks::where(["id" => $filter['work_id']])->update(['medal_type' => 0]);
            }
        }
        //获取权益
        $lit['list'] = (new Rightsandinterests())->get()->toArray();
        return $lit;
    }
    
    /**
     * @param $filter
     * 获取任务列表
     */
    public static function getTaskhallList($filter)
    {
        $where[] = ['states', '=', 1];
        if (isset($filter['work_id'])) {
            $list = self::where($where)->get()->toArray();
            $list_log = self::getUserTaskLogs($list, $filter['work_id']);
        } else {
            $list = self::where($where)->get()->toArray();
            $list_log = self::getUserTaskLogs($list);
        }
        return $list_log;

    }

    /**
     * @param $list
     * @param int $u_id
     * 获取用户任务记录
     */
    public static function getUserTaskLogs($list,$work_id=0)
    {
        if($work_id==0){
            foreach($list as $k=>&$val){
                $val['is_done'] = 0;
            }
        }else{
            foreach($list as $k=>&$val){
                $val['is_done'] = 0;
                if ($val['id'] == 1 || $val['id'] == 2 || $val['id'] == 3) { //每日签到或者分享
                    $date = date("Y-m-d", time());
                    $finish = TaskComplete::where([['task_id','=',$val['id']],['date_at','=',$date],['work_id','=',$work_id]])->first();
                    if($finish){
                        $val['is_done'] = 1;
                    }
                } else { //完成第一笔订单
                    $finish = TaskComplete::where([['task_id','=',$val['id']],['work_id','=',$work_id]])->first();
                    if($finish){
                        $val['is_done'] = 1;
                    }
                }
//                elseif ($val['id'] == 5) { //完成第一次图文问诊服务
//
//                } elseif ($val['id'] == 6) { //完成第一次视频销售
//
//                } elseif ($val['id'] == 7) { //收到第一次好评
//
//                }
            }
        }
        return $list;
        
    }


    
}
